package com.atguigu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spzx.common.utils.BeanUtils;
import com.atguigu.spzx.common.utils.CollectionUtils;
import com.atguigu.spzx.product.dto.ProductSkuDetailRespDto;
import com.atguigu.spzx.product.dto.SkuSaleNumUpdateReqDto;
import com.atguigu.spzx.product.entity.Product;
import com.atguigu.spzx.product.entity.ProductDetails;
import com.atguigu.spzx.product.entity.ProductSku;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductService;
import com.atguigu.spzx.product.vo.ProductDetailsRespVo;
import com.atguigu.spzx.product.vo.ProductSkuQueryReqVo;
import com.atguigu.spzx.product.vo.ProductSkuQueryRespVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * ProductServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2023/11/3
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    /**
     * 分页查询
     * @param pageParam
     * @param vo
     * @return
     */
    @Override
    public Page<ProductSkuQueryRespVo> getProductPageByQuery(Page<Product> pageParam, ProductSkuQueryReqVo vo) {
        return productSkuMapper.getProductPageByQuery(pageParam,vo);
    }

    /**
     * 商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductDetailsRespVo getProductSkuBySkuId(Long skuId) {
        ProductDetailsRespVo respVo = new ProductDetailsRespVo();

        //根据skuId获取商品sku信息
        ProductSku productSku = productSkuMapper.selectById(skuId);
        Long productId = productSku.getProductId();

        //根据productId查询Product信息
        Product product = productMapper.selectById(productId);


        //根据productId查询ProductDetails信息
        ProductDetails productDetails = productDetailsMapper.getProductDetailsByProductId(productId);

        //封装map，保存skuId和规格的对应关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        List<ProductSku> productSkuList = productSkuMapper.getProductSkuListByProductId(productId);
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        //封装数据
        ProductDetailsRespVo.ProductDetailsInnerProduct productDetailsInnerProduct = BeanUtils.toBean(product, ProductDetailsRespVo.ProductDetailsInnerProduct.class);
        respVo.setProduct(productDetailsInnerProduct);

        ProductDetailsRespVo.ProductDetailsInnerProductSku productDetailsInnerProductSku = BeanUtils.toBean(productSku, ProductDetailsRespVo.ProductDetailsInnerProductSku.class);
        respVo.setProductSku(productDetailsInnerProductSku);

        respVo.setSkuSpecValueMap(skuSpecValueMap);

        respVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));

        respVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));

        respVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));

        return respVo;
    }

    /**
     * 根据skuId查询sku信息
     * @param skuId
     * @return
     */
    @Override
    public ProductSkuDetailRespDto getProductSkuDtoBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.selectById(skuId);
        return BeanUtils.toBean(productSku, ProductSkuDetailRespDto.class);
    }

    /**
     * 更新商品sku销量
     * @param skuSaleNumUpdateReqDtoList
     */
    @Override
    public void updateSkuSaleNum(List<SkuSaleNumUpdateReqDto> skuSaleNumUpdateReqDtoList) {
        if(CollectionUtils.isNotEmpty(skuSaleNumUpdateReqDtoList)) {
            for(SkuSaleNumUpdateReqDto skuSaleNumUpdateReqDto : skuSaleNumUpdateReqDtoList) {
                productSkuMapper.updateSkuSaleNumBySkuId(skuSaleNumUpdateReqDto.getSkuId(), skuSaleNumUpdateReqDto.getSkuNum());
            }
        }
    }
}
